package com.shangyitong.yygh_user.controller;

import com.shangyitong.Result;
import com.shangyitong.ResultCodeEnum;
import com.shangyitong.exception.UserException;
import com.shangyitong.utils.AuthContextHolder;
import com.shangyitong.yygh.enums.AuthStatusEnum;
import com.shangyitong.yygh.model.user.UserInfo;
import com.shangyitong.yygh.vo.user.LoginVo;
import com.shangyitong.yygh.vo.user.UserAuthVo;
import com.shangyitong.yygh_user.service.UserInfoService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/api/user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 登录账号
     * finished
     * @param loginVo
     * @return
     */
    @ApiOperation(value = "登录账号")
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo){
        Map<String, Object> userInfo = userInfoService.login(loginVo);
        return Result.ok(userInfo);
    }

    /**
     * 用户实名认证
     * finished
     * @param userAuthVo
     * @param request
     * @return
     */
    @ApiOperation(value = "用户实名认证接口")
    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        userInfoService.userAuth(userId, userAuthVo);
        return Result.ok();
    }

    /**
     * 获取用户信息接口
     * finished
     * @param request
     * @return
     */
    @ApiOperation(value = "获取用户信息接口")
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        if (userId == null){
            throw new UserException(ResultCodeEnum.LOGIN_AUTH);
        }
        UserInfo userInfo = userInfoService.getById(userId);
        userInfo.getParam().put("authStatusString", AuthStatusEnum.getStatusNameByStatus(userInfo.getAuthStatus()));
        return Result.ok(userInfo);
    }

}
